<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class WriteXLS {
	private $styles = array ();
	private $colcountter = 0;
	var $widths;
	var $hstyles = null;
	private $filename = 'document-import';
	private $table;
	private $row = 0;
	private $index = - 1;
	private $open_tag = '{{';
	private $close_tag = '}}';
	private $start_header = false;
	function __construct() {
		setlocale ( LC_ALL, 'POSIX' );
		$this->table = array (
				'open' => $this->open_tag . 'table border="0" cellspacing="0" ' . $this->close_tag,
				'close' => $this->open_tag . '/table' . $this->close_tag 
		);
		
		// create obj
	}
	function add_style($name, $fontface, $fontsize, $fontstyle, $fontcollor, $fillcollor) {
		$this->styles [$name] = array (
				'fontface' => $fontface,
				'fontsize' => $fontsize,
				'fontstyle' => $fontstyle,
				'fontcollor' => $fontcollor,
				'fillcollor' => $fillcollor 
		);
	}
	function add_widths($widths) {
		$this->widths = $widths;
	}
	function get_page() {
		return 1;
	}
	/*
	 * aplicando o style @param $namestyle
	 */
	function apply_style($namestyle) {
		if (isset ( $this->styles [$namestyle] )) :
			$style = $this->styles [$namestyle];
			$align = isset ( $style ['align'] ) ? $style ['align'] : 'L';
			switch ($align) {
				case 'L' :
				default :
					$nalign = 'left';
					break;
				case 'R' :
					$nalign = 'right';
					break;
				case 'C' :
					$nalign = 'center';
					break;
			}
			return $str_style = ' style="background:' . $style ['fillcollor'] . '; font-size: ' . $style ['fontsize'] . 'pt; border:' . @$style ['border'] . ' solid #000; width:' . @$style ['width'] + 10 . ';" align="' . $nalign . '" bgcolor="' . $style ['fillcollor'] . '" ';
		
        endif;
	}
	function add_row() {
		if ($this->row > 0) {
			$this->table ['rows'] [$this->index] ['close_row'] = $this->open_tag . '/tr' . $this->close_tag;
			$this->row = 0;
		}
		$this->index += 1;
		$this->table ['rows'] [$this->index] ['open_row'] = $this->open_tag . 'tr' . $this->close_tag;
		$this->colcountter = 0;
		$this->row = 1;
	}
	function add_cell($data, $align, $style = null, $colspan = 1, $border = 1) {
		$width = 0;
		// calculate width from cells
		for($w = $this->colcountter; $w < $this->colcountter + $colspan; $w ++) {
			$width += $this->widths [$w];
		}
		if (isset ( $this->styles [$style] )) {
			$this->styles [$style] ['width'] = $width;
			$this->styles [$style] ['align'] = $align;
			$this->styles [$style] ['border'] = $border;
		}
		// apply style in cell
		$istyle = $this->styles [$style];
		$style = $this->apply_style ( $style );
		$size = ( int ) $istyle ['fontsize'] - 8;
		$fontsize = ($size > 0 ? $size : 1);
		$weight = substr ( $istyle ['fontstyle'], 0, 1 ) == 'B' ? 900 : 100;
		// show cell with @$data
		// $this->pdf->Cell($width, $fontsize - 4, $data, $border, 0, strtoupper(substr($align, 0, 1)), true);
		$index = $this->index;
		$this->table ['rows'] [$index] ['cells'] [] = $this->open_tag . 'td ' . $style . ' colspan = "' . $colspan . '"' . $this->close_tag . '<font color="' . @$istyle ['fontcollor'] . '" face="' . $istyle ['fontface'] . '" size="' . ($fontsize) . '" WEIGHT=' . $weight . '>' . $data . '</font>' . $this->open_tag . '/td' . $this->close_tag;
		$this->colcountter ++;
	}
	function save_file($name = '') {
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name;
		echo $this->parse ();
	}
	function add_header($data) {
		$this->table ['heading'] = $data;
	}
	function add_footer($data = '') {
		$this->table ['footer'] = $data;
	}
	function show_file($name = '') {
		// $this->filename =
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name;
		echo $this->parse ();
	}
	function start_header() {
		$this->start_header = true;
	}
	function end_header() {
		$this->start_header = false;
	}
	private function parse() {
		$this->set_headers ();
		if ($this->row > 0) {
			$this->table ['rows'] [$this->index] ['close_row'] = $this->open_tag . '/tr' . $this->close_tag;
			$this->row = 0;
		}
		
		$table = $this->parse_html_element ( $this->table ['open'] );
		
		if (isset ( $this->table ['heading'] ) && ! empty ( $this->table ['heading'] )) {
			$colspan = count ( $this->table ['rows'] ['0'] ['cells'] );
			$_hd = '';
			if (is_array ( $this->table ['heading'] )) {
				foreach ( $this->table ['heading'] as $hds ) :
					$_hd .= $hds;
				endforeach
				;
			}
			$this->table ['heading'] = $_hd;
			$table .= "<thead><tr><th colspan=\"{$colspan}\">{$this->table['heading']}</th></tr></thead>";
		}
		
		foreach ( $this->table ['rows'] as $row ) {
			$table .= $this->parse_html_element ( $row ['open_row'] );
			if (isset ( $row ['cells'] ) && count ( $row ['cells'] ) > 0) {
				foreach ( $row ['cells'] as $cell ) {
					$table .= $this->parse_html_element ( $cell );
				}
			}
			$table .= $this->parse_html_element ( $row ['close_row'] );
		}
		if (isset ( $this->table ['footer'] ) && ! empty ( $this->table ['footer'] )) {
			$colspan = count ( $this->table ['rows'] ['0'] ['cells'] );
			$table .= "<tfoot><tr><td colspan=\"{$colspan}\">{$this->table['heading']}</td></tr></tfoot";
		}
		$table .= $this->parse_html_element ( $this->table ['close'] );
		
		return $table;
	}
	function parse_html_element($element) {
		return str_replace ( array (
				$this->open_tag,
				$this->close_tag 
		), array (
				'<',
				'>' 
		), $element );
	}
	function download_file($name = '') {
		$name = ! empty ( $name ) ? $name : $this->filename;
		$this->filename = $name;
		echo $this->parse ();
	}
	static function convert_to_rgb($color) {
		$r = hexdec ( substr ( $color, 1, 2 ) );
		$g = hexdec ( substr ( $color, 3, 2 ) );
		$b = hexdec ( substr ( $color, 5, 2 ) );
		
		return array (
				'r' => $r,
				'g' => $g,
				'b' => $b 
		);
	}
	private function set_headers() {
		$ua = $_SERVER ["HTTP_USER_AGENT"];
		$filename = $this->filename . ".xls";
		$encoded_filename = urlencode ( $filename );
		$encoded_filename = str_replace ( "+", "%20", $encoded_filename );
		// ob_end_clean();
		// header("Pragma: public");
		// no-cache
		header ( "Pragma: no-cache" );
		
		header ( "Expires: 0" );
		header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		header ( "Content-Type: application/vnd.ms-excel; charset=UTF-8" );
		header ( "Content-Type: application/force-download" );
		header ( "Content-Type: application/octet-stream" );
		// header("Content-Type: application/vnd.ms-excel;charset=UTF-8");
		header ( "Content-Type: application/download" );
		if (preg_match ( "/MSIE/", $ua )) {
			header ( 'Content-Disposition: attachment; filename="' . $encoded_filename . '"' );
		} else if (preg_match ( "/Firefox/", $ua )) {
			header ( 'Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"' );
		} else {
			header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );
		}
		header ( "Content-Transfer-Encoding: binary " );
	}
}

?>
